/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.components;

import com.elf.gwt.common.client.model.ManufacturerModel;
import com.elf.gwt.store.client.StoreComponentController;
import com.elf.gwt.store.client.utils.StoreUtils;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

public class ManufacturerInfoComponent extends StoreSimpleComponent
{
	protected Grid bodyPanel = null;
	protected boolean manufacturerFound = false;

	/**
	 * Constructor
	 */
	public ManufacturerInfoComponent(StoreComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix, null, false);
		CellFormatter cf = panel.getCellFormatter();
		cf.setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER);


		bodyPanel = new Grid(3,1);
		bodyPanel.addStyleName("ComponentBodyPanel");
		bodyPanel.addStyleName(getHandle() + "BodyPanel");
		cf = bodyPanel.getCellFormatter();
		cf.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
		cf.setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER);
		cf.setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_CENTER);
		panel.setWidget(1, 0, bodyPanel);
	}
	
	public void onBuilt()
	{
		setVisible(manufacturerFound);
	}
	
	public void build()
	{
		int mId = StoreComponentController.productComponent.getProductModel().getManufacturerId();
		ManufacturerModel model = StoreComponentController.manufacturersComponent.getManufacturerModelById(mId);
		int id = 0;
		manufacturerFound = false;
		if (model != null) id = model.getId();
		if (id > 0)
		{
			manufacturerFound = true;
			Image image = new Image();
			image.setUrl(StoreUtils.getImageManufacturersDirectory() + model.getImage());
			bodyPanel.setWidget(0, 0, image);

			Anchor anchor = new Anchor(model.getName() + " " + storeController.getStoreConstants().ManufacturerInfoComponentHomepage(), false, model.getUrl());
			anchor.setStyleName("ComponentLink");
			bodyPanel.setWidget(1, 0, anchor);
			InlineLabel label = new InlineLabel (storeController.getStoreConstants().ManufacturerInfoComponentOther());
			label.setStyleName("ComponentLink");
			label.addClickHandler(searchClickHandler);
			bodyPanel.setWidget(2, 0, label);
		}
		else
		{
			bodyPanel.setText(0, 0, "");
			bodyPanel.setText(1, 0, "");
			bodyPanel.setText(2, 0, "");
		}
		
		onBuilt();
	}

	protected ClickHandler searchClickHandler = new ClickHandler() {
		public void onClick(ClickEvent event) {
			int pid = StoreComponentController.productComponent.getProductModel().getManufacturerId();
			onSelection("AdvancedSearchResultsComponent--0--"+ pid + "--false--true--true--0--0--*--*--0--*--*");
		}
	};
}
